package com.boot.modules.auth.controller;

import com.boot.common.anno.AnnoLog;
import com.boot.common.anno.RepeatSubmit;
import com.boot.common.result.Result;
import com.boot.modules.auth.service.AuthService;
import com.boot.util.IPUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    AuthService authService;
    
    /**
     * 用户登录
     * @param dataParam 登录请求参数
     * @param channel 渠道
     * @return 登录结果
     */
    @AnnoLog(value = "用户登录")
    @RepeatSubmit
    @PostMapping("/login")
    public Result<String> login(@RequestBody Map<String, Object> dataParam, @RequestHeader("Channel") String channel, HttpServletRequest request) {
        return authService.login(dataParam, channel, IPUtil.getIpAddr(request));
    }
    
    /**
     * 用户注册
     * @param dataParam 注册请求参数
     * @return 注册结果
     */
    @AnnoLog(value = "用户注册")
    @RepeatSubmit
    @PostMapping("/register")
    public Result<?> register(@RequestBody Map<String, Object> dataParam) {
        return authService.register(dataParam);
    }
    
    /**
     * 用户登出
     * @param channel 渠道
     * @return 登出结果
     */
    @AnnoLog(value = "用户登出")
    @GetMapping("/logout")
    public Result<?> logout(@RequestHeader("Channel") String channel) {
        return authService.logout(channel);
    }
}